package com.wc.codeforces.图论.Remove_Exactly_Two;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/5 18:00
 * @description
 * https://codeforces.com/contest/2063/problem/C
 */
public class Main {
    /**
     * 思路：
     * 需要删除连接边最多的两个顶点即可
     * 细节：
     * 删除最多边的时候, 然后才能选择第二个边的
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] h = new int[N], e = new int[N << 1], ne = new int[N << 1];
    static int[] d = new int[N];
    static boolean[] st = new boolean[N];
    static int n, idx, x, y;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            idx = 1;
            x = y = 0;
            n = sc.nextInt();
            Arrays.fill(st, 1, n + 1, false);
            Arrays.fill(h, 1, n + 1, 0);
            Arrays.fill(d, 1, n + 1, 0);
            for (int i = 1; i < n; i++) {
                int a = sc.nextInt(), b = sc.nextInt();
                add(a, b);
                add(b, a);
                d[a]++;
                d[b]++;
            }

            for (int i = 1; i <= n; i++) {
                if (d[x] < d[i]) x = i;
            }

            int res = 0;
            int id = 1;
            for (int k = 1; k <= n; k++) {
                if (d[x] == d[k]) {
                    x = k;
                    int tmp = 0;
                    tmp += d[x];
                    // 删除一下最多的顶点的边
                    for (int i = h[x]; i > 0; i = ne[i]) {
                        int j = e[i];
                        d[j]--;
                    }

                    y = 0;
                    for (int i = 1; i <= n; i++) {
                        if (i != x && d[y] <= d[i]) y = i;
                    }

                    if (d[y] == 0) tmp -= 1;
                    if (d[y] > 1) tmp += d[y] - 1;
                    // 复原
                    for (int i = h[x]; i > 0; i = ne[i]) {
                        int j = e[i];
                        d[j]++;
                    }
                    res = Math.max(res, tmp);
                    if (id ++ > 10) break;
                }
            }

            out.println(res);
        }
        out.flush();
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

